/*
-----------------------------------------------------------------------------
File:        LightManager.h
Copyright:   Copyright (C) 2007-2015 Poojan Prabhu. All rights reserved.
Created:     10/20/2015
Last edit:   12/15/2015
Author:      Poojan Prabhu
E-mail:      openig@compro.net

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#pragma once

#if defined(OPENIG_SDK)
    #include <OpenIG-Graphics/Export.h>
    #include <OpenIG-Graphics/ForwardDeclare.h>
    #include <OpenIG-Graphics/AxisAlignedBoundingBox.h>
    #include <OpenIG-Graphics/Light.h>
    #include <OpenIG-Graphics/OctreeFwdDeclare.h>
    #include <OpenIG-Graphics/OctreeNodeFwdDeclare.h>
    #include <OpenIG-Graphics/CameraFwdDeclare.h>
    #include <OpenIG-Graphics/Octree.h>
#else
    #include <Library-Graphics/Export.h>
    #include <Library-Graphics/ForwardDeclare.h>
    #include <Library-Graphics/AxisAlignedBoundingBox.h>
    #include <Library-Graphics/Light.h>
    #include <Library-Graphics/OctreeFwdDeclare.h>
    #include <Library-Graphics/OctreeNodeFwdDeclare.h>
    #include <Library-Graphics/CameraFwdDeclare.h>
    #include <Library-Graphics/Octree.h>
#endif

namespace OpenIG {
    namespace Library {
        namespace Graphics {

            class IGLIBGRAPHICS_EXPORT LightManager
            {
            public:
                LightManager(/*const AxisAlignedBoundingBox_64& box, size_t depth*/);
                virtual ~LightManager();
                Light*  CreateLight(LightType lightType);
                void    DestroyLight(Light* pLight);

                size_t  GetNumLights(void) const;
                Light*  GetLight(size_t index);

                // Get all the lights that have been registered/created
                const VectorLights& GetAllLights(void) const;

                // Call this before calling either of the 2 queries below
                void Update(Camera_64* pCamera);

                // Get all the lights that affect the view frustum. Must be called after LightManager::Update
                const VectorLights& GetFrustumAffectingLights(void) const;
            protected:

                ListLights m_Lights;

                mutable bool m_bVectorLightsdirty;
                mutable VectorLights m_VectorLights;

                Octree_64* m_Octree;
                void InitOctree(const AxisAlignedBoundingBox_64& box);

                void LightUpdated(const Light* pLight);
                void LightBoundsUpdated(const Light* pLight);
                void LightDestroyed(const Light* pLight);

                void ConnectSignals(Light* pSceneNode);
                void DisconnectSignals(const Light* pLight);

                void CreateOctreeNode(Light* pLight);
                void DestroyOctreeNode(Light* pLight);
                OctreeNode_64* GetOctreeNode(const Light* pLight) const;

                static const std::string m_StrOctreeNodeKey;
                static const std::string m_StrLightKey;

                void FindVisibleObjects(Camera_64* pCamera);
                Octree_64::NodeList m_VisibleOctreeNodes;
                VectorLights m_FrustumAffectingLights;
                DequeLights m_DequeLightsScratchPad;

                VectorLights m_FrustumAffectingLightsSortingScratchPad;
            };
        }
    }
}

